﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using androidpost.Utils;

namespace androidpost
{
    public partial class startcheck : Form
    {

        delegate void SetTextCallback(String a);
        public startcheck()
        {
            InitializeComponent();
        }
        Thread a;
        private void startcheck_Load(object sender, EventArgs e)
        {

            a = new Thread(new ThreadStart(read));
            a.IsBackground = false;
            a.Start();
          
        }

        private void read()
        {
             WebClient cGet = new WebClient();
             try
             {
                
                // cGet.Encoding = System.Text.Encoding.UTF8;


                 String a= cGet.DownloadString("https://sites.google.com/site/easyremoteppt/geturl/geturl.html");

                 a.Substring(a.IndexOf("http"));
                 Constant.domain = a;

                 String b = cGet.DownloadString(a+"getver.php");
                b= b.Substring(b.IndexOf("--")+2);
                
                if (b.CompareTo(Application.ProductVersion) > 0)
                {
                    MessageBox.Show("Đã có phiên bản mới!\n Cập nhật phiên bản để sủ dụng");
                    System.Diagnostics.Process.Start("http://naq.name.vn/erppt/");
                    Environment.Exit(1);
                    Application.ExitThread();
                }

             }
             catch (Exception)
             {

                 MessageBox.Show("connection error!");
                 try
                 {
                     //label1.Invoke((Action)(() => label1.Text = "Lỗi kết nối mạng"));
                     //label1.Text = "Lỗi kết nối mạng";
                     
                    // if (a != null) a.Abort(); // dừng thread kiểm tra
                     //this.Invoke((Action)(() => this.Close()));
                 }
                 catch (Exception)
                 {
                     
                     
                 }
                
             }
            
           
            SetText("a");
            
        }

         private void SetText(string text)
        {
            
            if (this.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.Close();
                
            }
        }

         private void startcheck_FormClosing(object sender, FormClosingEventArgs e)
         {
             if (a != null) a.Abort();
             
         }

    }
}
